package com.hxd.otc.base;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.view.View;

import com.hxd.otc.R;
import com.hxd.otc.base.permission.PermissionActivity;
import com.hxd.otc.dialog.PhotoDialog;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.other.OnPermissionsResult;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.FileUtils;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.UCropUtils;
import com.hxd.otc.widget.BlocksToast;
import com.yalantis.ucrop.UCrop;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 包  名 :  com.hxd.otc.base
 * 类  名 :  PhotoActivity
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/8 16:45
 * 描  述 :  ${相册相机选择Activity}
 * 首先必须先打开相机相册dialog,然后选择具体操作
 */
public abstract class PhotoActivity<P extends BasePresenter> extends PermissionActivity<P> {

    private PhotoDialog mPhotoDialog;
    private static final int PICK_IMAGE_REQUEST_CODE = 100;
    private static final int OPEN_CAMERA_REQUEST_CODE = 101;
    private File mCameraFile;
    private Uri mCameraUri;
    private boolean isCrop;//是否要裁剪
    public static final int DEFAULT_CHOOSE_MIN_IMAGE = 1;
    public static final int DEFAULT_CHOOSE_MAX_IMAGE = 9;


    protected void showPhotoDialog() {
        if (mPhotoDialog == null) {
            mPhotoDialog = new PhotoDialog(this);
        }
        if (!mPhotoDialog.isShowing()) {
            mPhotoDialog.show();
        }
        mPhotoDialog.setOnPhotoDialogItemClickListener(new PhotoDialog.OnPhotoDialogItemClickListener() {
            @Override
            public void onClickCamera(View view) {
                mPhotoDialog.dismiss();
                openDialogCamera();
                //  openCamera();
            }

            @Override
            public void onClickAlbum(View view) {
                mPhotoDialog.dismiss();
                openDialogAlbum();
                //打开相册
                //  openAlbum();
            }

            @Override
            public void onClickFee(View view) {

            }

            @Override
            public void onClickCancel(View view) {
                mPhotoDialog.dismiss();
            }
        });
    }

    protected void openAlbum(int maxImage) {
        if (maxImage > 0) {
            Matisse.from(this)
                    .choose(MimeType.ofImage())
                    .countable(true)
                    .maxSelectable(maxImage)
                    .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                    .thumbnailScale(0.85f)
                    .imageEngine(new GlideEngine())
                    .forResult(PICK_IMAGE_REQUEST_CODE);
        } else {
            BlocksToast.get().showToast(R.string.max_chooses_image);
        }
    }

    protected void openAlbum() {
        Matisse.from(this)
                .choose(MimeType.ofImage())
                .countable(true)
                .maxSelectable(DEFAULT_CHOOSE_MIN_IMAGE)
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                .thumbnailScale(0.85f)
                .imageEngine(new GlideEngine())
                .forResult(PICK_IMAGE_REQUEST_CODE);
    }


    protected void showPhotoDialog(boolean isCrop) {
        this.isCrop = isCrop;
        showPhotoDialog();
    }

    protected abstract void openDialogCamera();

    protected abstract void openDialogAlbum();


    protected void openCamera() {
        requestPermission(new OnPermissionsResult() {
            @Override
            public void onAllow(List<String> allowPermissions) {
                onCamera();
            }

            @Override
            public void onNoAllow(List<String> noAllowPermissions) {
                BlocksToast.get().showToast(getString(R.string.permisson5));
            }

            @Override
            public void onForbid(List<String> noForbidPermissions) {
                showForbidPermissionDialog();
            }

            @Override
            public void onLowVersion() {
                onCamera();
            }
        }, Manifest.permission.CAMERA);
    }

    private void onCamera() {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        if (cameraIntent.resolveActivity(getPackageManager()) != null) {
            mCameraFile = FileUtils.outImageFile(this, FileUtils.CAMERA);
            mCameraUri = FileUtils.fileToUri(this, mCameraFile, cameraIntent);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraUri);
            startActivityForResult(cameraIntent, PhotoActivity.OPEN_CAMERA_REQUEST_CODE);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                //拍照返回结果
                if (requestCode == PhotoActivity.OPEN_CAMERA_REQUEST_CODE) {
                    if (FileUtils.isImageFile(mCameraFile)) {
                        if (isCrop) {
                            UCropUtils.cropImage(mCameraUri, Uri.fromFile(FileUtils.outImageFile(this, FileUtils.CAMERA)), 1, 1
                                    , DataUtils.dp2px(this, 200), DataUtils.dp2px(this, 200), this);
                        } else {
                            FileUtils.scanImage(this, mCameraFile);
                            resultFiles(mCameraFile);
                        }
                    }
                    //裁剪图片返回结果
                } else if (requestCode == UCrop.REQUEST_CROP) {
                    Uri outImageUri = UCrop.getOutput(data);
                    if (outImageUri != null) {
                        resultFiles(new File(outImageUri.getPath()));
                    }
                    //相册返回
                } else if (requestCode == PICK_IMAGE_REQUEST_CODE) {
                    List<String> filePaths = Matisse.obtainPathResult(data);
                    if (filePaths != null && filePaths.size() > 0) {
                        List<File> resultFile = new ArrayList<>();
                        for (int i = 0; i < filePaths.size(); i++) {
                            resultFile.add(new File(filePaths.get(i)));
                        }
                        imageFileResult(resultFile);
                        LogUtils.w("requestCode--", resultFile.get(0) + "--" + resultFile.size());
                    }
                    //imageFileResult(new File(data.getData().getPath()));

                }
        }
    }

    private void resultFiles(@NonNull File file) {
        List<File> data = new ArrayList<>();
        data.add(file);
        imageFileResult(data);
    }

    /**
     * 图片返回结果
     *
     * @param imageFiles 返回图片结果集合
     */
    protected abstract void imageFileResult(@NonNull List<File> imageFiles);

}
